package com.bc.bcapi.service;

import com.bc.bcapi.dao.*;
import com.bc.bcmodel.domain.*;
import com.bc.bcmodel.enums.IntegralRecordEnum;
import com.bc.bcmodel.req.AnalysisSetReq;
import com.bc.bcmodel.req.ProjectAnalysisListReq;
import com.bc.bcmodel.req.ProjectAnalysisSetGoodReq;
import com.bc.bcmodel.req.UserElitesApplyReq;
import com.bc.bcmodel.resp.ProjectAnalysisListBaseResp;
import com.bc.bcmodel.resp.ProjectAnalysisListResp;
import com.bc.commoncore.util.DateUtil;
import com.bc.commoncore.util.PageInfoExt;
import com.bc.commoncore.util.ParseHelper;
import com.bc.commoncore.util.StringUtils;
import com.bc.commonentity.common.JsonResult;
import com.bc.commonentity.enums.HttpReturnRnums;
import com.bc.commonentity.enums.SeqNameEnum;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.math.BigInteger;
import java.util.stream.Collectors;

import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Name T_Project_analysisService * @Description  * @Author generation * @Date 2018/8/29 12:57:27 * @Version 1.0
 **/
@Service
public class T_Project_analysisService {
    @Resource
    T_Project_analysisMapper t_Project_analysisMapper;
    @Resource
    T_User_buyer_infoMapper t_user_buyer_infoMapper;
    @Resource
    T_Project_analysis_opt_logMapper t_project_analysis_opt_logMapper;
    @Resource
    T_SequenceService t_sequenceService;
    @Resource
    T_User_joinMapper t_user_joinMapper;
    @Resource
    T_Project_analysis_relationMapper t_project_analysis_relationMapper;
    @Resource
    T_User_elitesMapper t_user_elitesMapper;
    @Resource
    T_Integral_recordService t_integral_recordService;

    /**
     * 保存单条数据     *      * @return
     */
    public int insert(T_Project_analysisModel req) {
        return t_Project_analysisMapper.insert(req);
    }

    /**
     * 批量增加数据     *      * @return
     */
    public int insertBatch(List<T_Project_analysisModel> req) {
        return t_Project_analysisMapper.insertBatch(req);
    }

    /**
     * 根据对象属性查找满足条件的第一条数据     *      * @return
     */
    public T_Project_analysisModel findByNamedParam(T_Project_analysisModel req) {
        return t_Project_analysisMapper.findByNamedParam(req);
    }

    /**
     * 根据对象查询所有数据     *      * @return
     */
    public List<T_Project_analysisModel> getAllList(T_Project_analysisModel req) {
        return t_Project_analysisMapper.findByNamedParamList(req);
    }

    /**
     * 根据对象查询所有分页数据     *      * @return
     */
    public PageInfo<T_Project_analysisModel> getList(T_Project_analysisModel req) {
        PageHelper.startPage(req.getPage_index(), req.getPage_size());
        List<T_Project_analysisModel> list = t_Project_analysisMapper.findByNamedParamList(req);
        return new PageInfo<T_Project_analysisModel>(list);
    }

    /**
     * 更新指定对象     *      * @return
     */
    public int update(T_Project_analysisModel req) {
        return t_Project_analysisMapper.update(req);
    }

    /**
     * 删除指定数据     *      * @return
     */
    public int delete(BigInteger id) {
        return t_Project_analysisMapper.delete(id);
    }

    /**
     * 根据条件统计数据量     *      * @return
     */
    public long count(T_Project_analysisModel req) {
        return t_Project_analysisMapper.count(req);
    }

    /**
     * 获取业务分析+代码分析列表
     */
    public JsonResult getAnalysisList(ProjectAnalysisListReq req) {
        JsonResult js = new JsonResult();
        PageHelper.startPage(req.getPage_index(), req.getPage_size());
        List<ProjectAnalysisListBaseResp> list = t_Project_analysisMapper.getAnalysisList(req);
        int count = 0;
        if (!StringUtils.isEmpty(req.getUser_id())) {
            count = t_Project_analysisMapper.getAnalysisCountByUserIdPid(req);
        }

        PageInfo<ProjectAnalysisListBaseResp> pageInfo = new PageInfo<ProjectAnalysisListBaseResp>(list);
        ProjectAnalysisListResp resp = new ProjectAnalysisListResp();
        resp.setIs_show_update(count > 0 ? 1 : 0);
        resp.setListBaseRespList(pageInfo);
        js.setData(resp);
        return js;
    }

    /**
     * 发布/编辑分析
     */
    @Transactional(rollbackFor = {
            Exception.class}, isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED)
    public JsonResult setAnalysis(AnalysisSetReq req) {
        JsonResult js = new JsonResult();
//        所属类型:1项目分析 2代码分析
        if (req.getType_id() == null || req.getPid() == null ||
                req.getAdesc().isEmpty() || req.getAdesc().length() > 1000) {
            return new JsonResult(HttpReturnRnums.ParaError);
        }
        String user_id = req.getUser_id();
        //查询一下当前用户的审核状态-开始
        T_User_elitesModel t_user_elitesModel = new T_User_elitesModel();
        t_user_elitesModel.setUser_id(user_id);
        T_User_elitesModel resultModel = t_user_elitesMapper.findByNamedParam(t_user_elitesModel);
        if (resultModel == null ||
                (req.getType_id() == 1 && resultModel.getIs_business_status() != 1) ||
                (req.getType_id() == 2 && resultModel.getIs_code_status() != 1)
                ) {
            return new JsonResult(HttpReturnRnums.USER_ELITS_AUTH_ERROR);
        }
        //查询AID
        String aid = t_Project_analysisMapper.getAidByPid(req);
        req.setAid(aid);
        //查询一下当前用户的审核状态-结束
        T_Project_analysisModel t_project_analysisModel = new T_Project_analysisModel();
        t_project_analysisModel.setPid(req.getPid());
        t_project_analysisModel.setAdesc(req.getAdesc());
        t_project_analysisModel.setAid(req.getAid());
        t_project_analysisModel.setType_id(req.getType_id());
        t_project_analysisModel.setUser_id(req.getUser_id());
        t_project_analysisModel.setVideo_url(req.getVideo_url());
        t_project_analysisModel.setAid(aid);
        int result = 0;
        if (!StringUtils.isEmpty(aid)) {
            //编辑
            t_project_analysisModel.setModify_time(DateUtil.getToday());
            result = t_Project_analysisMapper.update(t_project_analysisModel);
        }
        if (result == 0) {
            //添加
            aid = t_sequenceService.genSequence(SeqNameEnum.PROJECT_ANALYSIS_AID);
            t_project_analysisModel.setAid(aid);
            t_Project_analysisMapper.insert(t_project_analysisModel);
            /*
             * 添加我参与的ICO
             * */
            T_User_joinModel t_user_joinModel = new T_User_joinModel();
            t_user_joinModel.setUser_id(req.getUser_id());
            t_user_joinModel.setPid(req.getPid());
            //1 我打标的ICO 2我参标的ICO 3我业务分析的ICO 4我代码分析的ICO
            t_user_joinModel.setType_id(req.getType_id().equals(1) ? 3 : 4);
            t_user_joinMapper.insert(t_user_joinModel);
            /**
             * 为专家表里代码分析、业务分析里增加数量
             * */
            t_user_elitesMapper.updateAnalysisCount(req);
            //添加分析积分
            addAnalysisIntegral(req.getType_id(), aid, req.getUser_id());
        }
        /**
         * 记录操作日志
         * */
        T_Project_analysis_opt_logModel t_project_analysis_opt_logModel =
                new T_Project_analysis_opt_logModel();
        t_project_analysis_opt_logModel.setAid(aid);
        t_project_analysis_opt_logModel.setPid(req.getPid());
        t_project_analysis_opt_logModel.setUser_id(req.getUser_id());
        t_project_analysis_opt_logModel.setAdesc(req.getAdesc());
        t_project_analysis_opt_logModel.setType_id(req.getType_id());
        t_project_analysis_opt_logMapper.insert(t_project_analysis_opt_logModel);
        return js;
    }

    /**
     * 业务分析+代码分析积分
     */
    private void addAnalysisIntegral(int type_id, String aid, String user_id) {

        if (type_id == 1) {
            //业务分析积分
            t_integral_recordService.threadInsertIntegralRecord(
                    IntegralRecordEnum.YWFXICO, aid, user_id);
        } else if (type_id == 2) {
            //代码分析积分
            t_integral_recordService.threadInsertIntegralRecord(
                    IntegralRecordEnum.DMFXICO, aid, user_id);
        }
    }

    /**
     * 设置好评
     */
    public JsonResult setGood(ProjectAnalysisSetGoodReq req) {
        JsonResult js = new JsonResult();
        //好评加1,
        T_Project_analysis_relationModel t_project_analysis_relationModel
                = new T_Project_analysis_relationModel();
        t_project_analysis_relationModel.setPid(req.getPid());
        t_project_analysis_relationModel.setAid(req.getAid());
        t_project_analysis_relationModel.setUser_id(req.getUser_id());

        int counter = t_project_analysis_relationMapper.count(t_project_analysis_relationModel);
        if (counter > 0) {
            return new JsonResult(HttpReturnRnums.PROJECT_EXISTS_FOLLOW_GOOD);
//            if (req.getIs_good() == 1) {
//                1好评，0差评
//            }
//            if (req.getIs_good() == 0) {
//                return new JsonResult(HttpReturnRnums.PROJECT_EXISTS_FOLLOW_DESPISE);
//            }
        }
        //评价加积分
        addSetGoodIntegral(req.getIs_good(), req.getPid(), req.getUser_id());
        t_project_analysis_relationModel.setIs_good(req.getIs_good());
        t_project_analysis_relationMapper.insert(t_project_analysis_relationModel);
        t_Project_analysisMapper.updateGoodCount(req);
        return js;
    }

    /**
     * 设置好差评积分
     */
    private void addSetGoodIntegral(int is_good, String pid, String user_id) {
        if (is_good == 1) {
            //好评积分
            t_integral_recordService.threadInsertIntegralRecord(
                    IntegralRecordEnum.DZZYFX, pid, user_id
            );
        } else if (is_good == 0) {
            //差评积分
            t_integral_recordService.threadInsertIntegralRecord(
                    IntegralRecordEnum.CZYFX, pid, user_id
            );
        }
    }
}